<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020-8-11
 * Time: 11:59
 */

namespace App\Member\Renderables;

use Illuminate\Contracts\Support\Renderable;
use Dcat\Admin\Admin;


class TeamRenderables implements Renderable
{

    public static $js = [
        'layui/layui.js',
        'ftp/zh.js',
        //'tree/tree.js',
    ];

    public static $css = [
        'layui/css/layui.css',
    ];


    public function teamScript()
    {
        $token               = csrf_token();
        $url_team_data       = route('admin_user.team_data');
        $url_my_money        = route('admin_user.my_money');
        $admin_user_zgjejlxx = trans('handlers.admin_user_zgjejlxx');
        $admin_user_gbxxxxxx = trans('handlers.admin_user_gbxxxxxx');
        $admin_user_wlsjlxxx = trans('handlers.admin_user_wlsjlxxx');


        return <<<JS
            layui.use(['tree','laydate'], function () {
                const $ = layui.$,
                    tree = layui.tree,
                    laydate = layui.laydate;


                //自定义格式
                laydate.render({
                    elem: '#test16'
                    ,range: '/'
                    ,format: 'yyyy-M-d'
                });

                var _token = "$token";

                // 点击加载数据
                $('.search-btn').click(function(){
                    var account = $('#account').val() ? $('#account').val() : '';
                    var times = $('#test16').val();

                    layer.load(1 , {shade:0.3});
                    $.ajax({
                        url:"$url_team_data",
                        type:'POST',
                        data:{account:account,times:times,type:1,_token:_token},
                        success:function(e){
                            $('#test1').text('');

                            layer.closeAll();
                            if (e.code == 0){
                                var data = JSON.parse(e.data);
                                //常规用法
                                var inst2 = tree.render({
                                    elem: '#test1' //默认是点击节点可进行收缩
                                    ,data: [data]
                                    ,id: 'treeid2' //定义索引
                                    ,drag: false
                                    ,onlyIconControl: true
                                    ,click: function(item){ //点击节点回调
                                    }
                                    ,spread: function (obj) {
                                        $.ajax({
                                            url:"$url_team_data",
                                            type:'POST',
                                            data:{account:obj.data.account,times:times,type:2,_token:_token},
                                            success:function(res){
                                                if(res.code < 0) {
                                                    $('div.layui-tree-init').empty()// 有BUG点击收缩不可用
                                                    return layer.msg(res.message , {times:1000});
                                                }
                                                var data = JSON.parse(res.data);
                                                tree.lazytree(inst2.config.id, obj.elem, data);
                                            }
                                        });
                                    }
                                });
                            }else{
                                layer.msg(e.message , {times:1000});
                            }
                        }
                    });
                });

                //打开页面加载 数据
                locaTree = function(){
                    layer.load(1 , {shade:0.3});
                    $.ajax({
                        url:"$url_team_data",
                        type:'POST',
                        data:{account:'', type:1, _token:_token},
                        success:function(e){
                            var data = JSON.parse(e.data);

                            layer.closeAll();
                            //常规用法
                            var inst1 = tree.render({
                                elem: '#test1' //默认是点击节点可进行收缩
                                ,data: [data]
                                ,id: 'treeid2' //定义索引
                                ,drag: false
                                ,onlyIconControl: true
                                ,click: function(item){ //点击节点回调
                                }
                                ,spread: function (obj) {
                                    $.ajax({
                                        url:"$url_team_data",
                                        type:'POST',
                                        data:{account:obj.data.account, type:2, _token:_token},
                                        success:function(res){
                                            if(res.code < 0) {
                                                $('div.layui-tree-init').empty()// 有BUG点击收缩不可用
                                                return layer.msg(res.message , {times:1000});
                                            }
                                            var data = JSON.parse(res.data);
                                            console.log(data);
                                            tree.lazytree(inst1.config.id, obj.elem, data);
                                        }
                                    });
                                }
                            });
                        }
                    });
                };
                locaTree();

                // 点击查看 用户自购金额流水
                $(window).off('click').on('click', '.spanId' , function () {
                //$(document).off('click').on('click', '.spanId' , function () {
                    var uid = $(this).attr('data-id');

                    $.ajax({
                        url:"$url_my_money",
                        type:'POST',
                        //dataType: 'json',
                        data:{admin_user:uid, _token:_token},
                        success:function(res){
                            if (res.code == 1){
                                layer.alert(res.data,{
                                    area:['500px','400px'],
                                    title: "$admin_user_zgjejlxx",
                                    shadeClose:true,
                                    btn:["$admin_user_gbxxxxxx"],
                                });
                            }else if(res.code == 2){
                                layer.msg(res.message);
                            }else{
                                layer.msg("$admin_user_wlsjlxxx");
                            }
                        }
                    });
                });
            });
            zh_tran('t')
JS;
    }


    public function render()
    {
        // 在这里可以引入你的js或css文件
        Admin::js(static::$js);
        Admin::css(static::$css);

        // 需要在页面执行的JS代码
        // 通过 Admin::script 设置的JS代码会自动在所有JS脚本都加载完毕后执行
        Admin::script($this->teamScript());

        return view('member.team.index')->render();
    }


}
